function rangeAdd(){
     $("#rangeItemContent").append(
     	"<div class='rangeItem'>" + 
     	"From <input type='text' name='ranges_min' class='integerText' />" + 
     	" To <input  type='text' name='ranges_max' class='integerText' />" + 
     	"  <a href='#' class='rangeAdd' onclick='rangeAdd()' ><img class='buttonBcImg' width='25' src='./images/add.png' /></a>" + 
     	"  <a href='#' class='rangeRemove' onclick='rangeDelete(this)' ><img class='buttonBcImg' width='25' src='./images/delete.png' /></a>" + 
     	"</div>"
     );
     
     if($("div#rangeDesContent").html().trim().length == 0){
     
     	$("input#B_rangeAdd").hide();
     	$("#rangeDesContent").append(
     		"Description:<br/>"
            +"<textarea class='intextArea'  id='range_description' name='range_description'></textarea>"
            +"Reference:<br/>"
            +"<textarea class='intextArea'  id='range_reference'   name='range_reference'></textarea>"    
     	);
     }
 }
 
 function patternAdd(){
 	 $("#patternItemContent").append(
 	    "<div class='patternItem'>"
 	 	+  "<div id='pattern_title'>"
        +     "<div class='patternLeft'>Pattern Name:</div>"
        +     "<div class='patternRight'>"
        +       "<a href='#' class='rangeRemove' onclick='patternDelete(this)' ><img class='buttonBcImg' width='20' src='./images/delete.png' /></a>"
		+    	"<a href='#' class='rangeAdd' onclick='patternAdd()' ><img class='buttonBcImg' width='20' src='./images/add.png' /></a>"
	    +	  "</div>"
	    +	  "<div style='clear:both;'></div>"
        +  "</div>"
        +"<textarea class='intextArea'  id='pattern_name' name='pattern_name'></textarea>"
        +"Description:<br/>"
        +"<textarea class='intextArea'  id='pattern_description' name='pattern_description'></textarea>"
        +"Reference:<br/>"
        +"<textarea class='intextArea'  id='pattern_reference' name='pattern_reference'></textarea>"
        +"</div>"
 	 );
 	  $("input#B_patternAdd").hide();
 }
  
 function rangeDelete(obj){
    $(obj).parent().remove();
    if( $("div#rangeItemContent").html().trim().length == 0){
    	$("input#B_rangeAdd").show();
    	$("#rangeDesContent").html("");
    }
 }
 
 function patternDelete(obj){
    $(obj).parent().parent().parent().remove();
    //去掉多余的空格，可兼容IE和Firefox
    if( $("div#patternItemContent").html().trim().length == 0){$("input#B_patternAdd").show();}
 }
 
 function getRangeStr(){
 	if($("div#rangeItemContent").html().trim().length == 0){
 		return true;
 	}
 	var range_min = document.getElementsByName("ranges_min");
	var renge_max = document.getElementsByName("ranges_max");
	var reangeStr = "";
	$("input.integerText").css("border-color","");
	for(i = 0; i < range_min.length; i++ ){
	    //alert(range_min[i].value + "--" +renge_max[i].value);
		if("" == range_min[i].value || isNaN(range_min[i].value)){
			alert("Enter numbers!");
			$(range_min[i]).css("border-color","red");
			$(range_min[i]).focus();
			return false;
		}else if( "" == renge_max[i].value || isNaN(renge_max[i].value)){
			alert("Enter numbers!");
			$(renge_max[i]).css("border-color","red");
			$(renge_max[i]).focus();
			return false;
		}else if((range_min[i].value - renge_max[i].value) > 0 ){
			alert("Enter correct numbers!");
			$(range_min[i]).css("border-color","red");
			$(renge_max[i]).css("border-color","red");
			$(range_min[i]).focus();
			return false;
		}
		//检查，多个range之间不能有重复，且必须是升序的
		if(i>0){
			if(range_min[i].value - renge_max[i-1].value < 0){
				alert("多个range之间不能有重叠，且必须是升序排列的!");
				$(range_min[i]).css("border-color","red");
				$(renge_max[i]).css("border-color","red");
				$(range_min[i]).focus();
				return false;
			}
		}
		if(i == 0){
			if((range_min[i].value - renge_max[i].value) == 0){
				reangeStr += range_min[i].value;
			}else{
				reangeStr += range_min[i].value + ".." + renge_max[i].value;
			}
		}else{
			if((range_min[i].value - renge_max[i].value) == 0){
				reangeStr += " | " +  range_min[i].value;
			}else{
				reangeStr += " | " + range_min[i].value + ".." + renge_max[i].value;
			}
		}
	}
	
	if("" != document.getElementById("range_description").value){
		reangeStr += "[rangeDes]" + document.getElementById("range_description").value;
	}
	
	if("" != document.getElementById("range_reference").value){
		reangeStr += "[rangeRef]" + document.getElementById("range_reference").value;
	}
	
 	document.getElementById("range_para").value=reangeStr;
 	//alert(reangeStr);
 	return true;
 }
 
 function getPatternStr(){
 	if($("div#patternItemContent").html().trim().length == 0){
 		return true;
 	}
 	var pattern_name = document.getElementsByName("pattern_name");
	var pattern_desc = document.getElementsByName("pattern_description");
	var pattern_refe = document.getElementsByName("pattern_reference");
	var patternStr   = "";
	var i = 0;
	for(i = 0 ; i < pattern_name.length ; i++){
		if("" == pattern_name[i].value ){
			alert("Pattern name can't be empty!");
			$(pattern_name[i]).focus();
			return false;
		}
		patternStr = patternStr + pattern_name[i].value ;
		patternStr = patternStr +  (("" == pattern_desc[i].value)?"":"[dessep]"+pattern_desc[i].value);
		patternStr = patternStr + (("" == pattern_refe[i].value)?"":"[refsep]"+pattern_refe[i].value);
	    
	    //最后一个不需要加分隔符
		if(i != pattern_name.length - 1){
			patternStr += "[pattsep]";
		}
	}
	
	if( "" == patternStr){
		alert("pattern can't be empty!");
		patternAdd()
		return false;
	}
	
 	document.getElementById("patterns").value=patternStr;
	//alert(patternStr);
	return true;
 }
 
 //解决IE中trim()不能使用的问题
 String.prototype.trim = function () {
	return this .replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' );
 }